<?php

namespace Health\Http\Requests;

use Illuminate\Http\JsonResponse;


class GoodsRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'img1' => $this->file('img1') ? 'image|max:2000' : '',
            'img2' => $this->file('img2') ? 'image|max:2000' : '',
            'img3' => $this->file('img3') ? 'image|max:2000' : '',
            'goods_category_id' => 'required',
            'goods_name' => 'required',
            'goods_title' => 'required',
            'price' => 'required|numeric',
            'sell_price' => 'required|numeric',
            'stock' => 'required|int'
        ];
    }

    public function messages()
    {
        return [
            'img1.image' => '只能上传图片文件',
            'img1.max' => '图片1太大了,只能传2MB',

            'img2.image' => '只能上传图片文件',
            'img2.max' => '图片2太大了,只能传2MB',

            'img3.image' => '只能上传图片文件',
            'img3.max' => '图片3太大了,只能传2MB',


            'goods_category_id.required' => '请选择商品分类',

            'goods_name.required'=>'请填写商品名称',

            'goods_title.required'=>'请填写简要说明',

            'price.required' => '请填写成本价',
            'price.numeric' => '成本价填写不正确,请保留两位小数点',

            'sell_price.required' => '请填写成本价',
            'sell_price.numeric' => '成本价填写不正确,请保留两位小数点',

            'stock.required' => '请填写库存数量',
            'stock.in' => '库存非法'

        ];
    }

    public function response(array $errors)
    {
        if ($this->ajax() || $this->wantsJson()) {
            return new JsonResponse(array('status' => 0, 'msg' => current($errors)[0]));
        }
        return $this->redirector->to($this->getRedirectUrl())
            ->withInput($this->except($this->dontFlash))
            ->withErrors($errors, $this->errorBag);
    }
}
